#include "Billboard.h"

Billboard::Billboard()
{
	SetSize(1.0f);
	maxframes = 0;

	SetSmoothAnim(0);
}

Billboard::Billboard(float size)
{
	SetSize(size);
	maxframes = 0;

	SetSmoothAnim(0);
}
Billboard::Billboard(float w,float h)
{
	SetSize(w,h);
	maxframes = 0;

	SetSmoothAnim(0);
}

Billboard::~Billboard()
{
}

void Billboard::SetSize(float sz)
{
	width = sz;
	height = sz;
}

void Billboard::SetSize(float w,float h)
{
	width = w;
	height = h;
}

void Billboard::SetWidth(float w)
{
	width = w;
}
void Billboard::SetHeight(float h)
{
	height = h;
}

float Billboard::GetSize()
{
	return sqrt(width*width + height*height);
}

float Billboard::GetWidth()
{
	return width;
}
float Billboard::GetHeight()
{
	return height;
}

void Billboard::Draw(float frame)
{
	float size = width;

	if(maxframes>0)
	{
		unsigned int iframe = (unsigned int)frame;
		iframe = iframe % maxframes;

		float suv = (float)iframe / (float)maxframes;
		float evw = (float)(iframe+1) / (float)maxframes;

		if(smoothanim)
		{
			sys::renderedtris+=4;
			sys::renderedverts+=8;

			float scalar = frame - iframe;
			float nvw = (float)(iframe+2) / (float)maxframes;


			glBegin(GL_QUADS);
			glColor4f(1,1,1,1-scalar);
			glTexCoord2f(suv,0);
			glVertex3f( cams::v1.x*size, cams::v1.y*size, cams::v1.z*size);
			
			glTexCoord2f(evw,0);
			glVertex3f( cams::v2.x*size, cams::v2.y*size, cams::v2.z*size);
			
			glTexCoord2f(evw,1);
			glVertex3f(-cams::v1.x*size,-cams::v1.y*size,-cams::v1.z*size);
			
			glTexCoord2f(suv,1);
			glVertex3f(-cams::v2.x*size,-cams::v2.y*size,-cams::v2.z*size);


			glColor4f(1,1,1,scalar);
			glTexCoord2f(evw,0);
			glVertex3f( cams::v1.x*size, cams::v1.y*size, cams::v1.z*size);
			
			glTexCoord2f(nvw,0);
			glVertex3f( cams::v2.x*size, cams::v2.y*size, cams::v2.z*size);
			
			glTexCoord2f(nvw,1);
			glVertex3f(-cams::v1.x*size,-cams::v1.y*size,-cams::v1.z*size);
			
			glTexCoord2f(evw,1);
			glVertex3f(-cams::v2.x*size,-cams::v2.y*size,-cams::v2.z*size);
			
			glEnd();

		}
		else
		{
			sys::renderedtris+=2;
			sys::renderedverts+=4;

			glBegin(GL_QUADS);

			glTexCoord2f(suv,0);
			glVertex3f( cams::v1.x*size, cams::v1.y*size, cams::v1.z*size);
			
			glTexCoord2f(evw,0);
			glVertex3f( cams::v2.x*size, cams::v2.y*size, cams::v2.z*size);
			
			glTexCoord2f(evw,1);
			glVertex3f(-cams::v1.x*size,-cams::v1.y*size,-cams::v1.z*size);
			
			glTexCoord2f(suv,1);
			glVertex3f(-cams::v2.x*size,-cams::v2.y*size,-cams::v2.z*size);
			
			glEnd();
		}
	}
	else
	{
		sys::renderedtris+=2;
		sys::renderedverts+=4;

		glBegin(GL_QUADS);

		glTexCoord2f(0,0);
		glVertex3f( cams::v1.x*size, cams::v1.y*size, cams::v1.z*size);
		
		glTexCoord2f(1,0);
		glVertex3f( cams::v2.x*size, cams::v2.y*size, cams::v2.z*size);
		
		glTexCoord2f(1,1);
		glVertex3f(-cams::v1.x*size,-cams::v1.y*size,-cams::v1.z*size);
		
		glTexCoord2f(0,1);
		glVertex3f(-cams::v2.x*size,-cams::v2.y*size,-cams::v2.z*size);

		glEnd();
	}
}

void Billboard::SetRenderMode(BRMode m)
{
	mode = m;
}

BRMode Billboard::GetRenderMode()
{
	return mode;
}

void Billboard::AnimLenght(unsigned int maxframes_)
{
	maxframes = maxframes_;
}

unsigned int Billboard::GetAnimLenght()
{
	return maxframes;
}

void Billboard::SetSmoothAnim(int smooth)
{
	smoothanim = smooth;
}